using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Buddy.Coroutines;
using log4net;
using Triton.Common.LogUtilities;
using Triton.Common.Math;
using Triton.Game.Mapping;

namespace Triton.Game
{
	public static class Client
	{
		private static readonly ILog ilog_0 = Triton.Common.LogUtilities.Logger.GetLoggerInstanceForType();

		public static readonly Random Random = new Random(Environment.TickCount ^ (Environment.MachineName.GetHashCode() + Environment.CurrentManagedThreadId));

		public static int DelayAfterMouseMove = 50;

		public static int DelayAfterMouseClick = 100;

		private static IntPtr intptr_0 = IntPtr.Zero;

		public static void ClickLMB()
		{
			PegUI.Get()?.OnAppFocusChanged(focus: true, intptr_0);
			Input.PressLMB();
			PegUI.Get()?.OnAppFocusChanged(focus: true, intptr_0);
			Input.ReleaseLMB();
			using (TritonHs.Memory.ReleaseFrame(reacquireAsHardLock: true))
			{
				Thread.Sleep(DelayAfterMouseClick);
			}
		}

		public static void ClickRMB()
		{
			PegUI.Get()?.OnAppFocusChanged(focus: true, intptr_0);
			Input.PressRMB();
			PegUI.Get()?.OnAppFocusChanged(focus: true, intptr_0);
			Input.ReleaseRMB();
			using (TritonHs.Memory.ReleaseFrame(reacquireAsHardLock: true))
			{
				Thread.Sleep(DelayAfterMouseClick);
			}
		}

		private static void smethod_0(int int_0, int int_1, bool bool_0)
		{
			PegUI.Get()?.OnAppFocusChanged(focus: true, intptr_0);
			Input.SetMousePos(int_0, int_1);
			using (TritonHs.Memory.ReleaseFrame(reacquireAsHardLock: true))
			{
				Thread.Sleep(DelayAfterMouseMove);
			}
			if (bool_0)
			{
				ClickLMB();
				return;
			}
			using (TritonHs.Memory.ReleaseFrame(reacquireAsHardLock: true))
			{
				Thread.Sleep(DelayAfterMouseClick);
			}
		}

		private static void smethod_1(int int_0, int int_1, bool bool_0)
		{
			PegUI.Get()?.OnAppFocusChanged(focus: true, intptr_0);
			Input.SetMousePos(int_0, int_1);
			using (TritonHs.Memory.ReleaseFrame(reacquireAsHardLock: true))
			{
				Thread.Sleep(DelayAfterMouseMove);
			}
			if (bool_0)
			{
				ClickRMB();
				return;
			}
			using (TritonHs.Memory.ReleaseFrame(reacquireAsHardLock: true))
			{
				Thread.Sleep(DelayAfterMouseClick);
			}
		}

		private static void smethod_2(Camera camera_0, Triton.Game.Mapping.Vector3 vector3_0, bool bool_0)
		{
			if (camera_0 != null)
			{
				Triton.Game.Mapping.Vector3 vector = camera_0.WorldToScreenPoint(vector3_0);
				//ilog_0.InfoFormat("[ClickPositionInfo] Position :{0},{1},{2}", (int)vector.X, Screen.Height - (int)vector.Y, bool_0);
				smethod_0((int)vector.X, Screen.Height - (int)vector.Y, bool_0);
			}
		}

		private static void smethod_3(Camera camera_0, Triton.Game.Mapping.Vector3 vector3_0, bool bool_0)
		{
			if (camera_0 != null)
			{
				Triton.Game.Mapping.Vector3 vector = camera_0.WorldToScreenPoint(vector3_0);
				smethod_1((int)vector.X, Screen.Height - (int)vector.Y, bool_0);
			}
		}

		public static void LeftClickAt(int cx, int cy)
		{
			smethod_0(cx, cy, bool_0: true);
		}

		public static void LeftClickAt(Triton.Game.Mapping.Vector3 center)
		{
			smethod_2(Camera.Main, center, bool_0: true);
		}

		public static void LeftClickAtDialog(Triton.Game.Mapping.Vector3 center)
		{
			smethod_2(BaseUI.Get().GetBnetCamera(), center, bool_0: true);
		}

		public static void RightClickAt(int cx, int cy)
		{
			smethod_1(cx, cy, bool_0: true);
		}

		public static void RightClickAt(Triton.Game.Mapping.Vector3 center)
		{
			smethod_3(Camera.Main, center, bool_0: true);
		}

		public static void RightClickAtDialog(Triton.Game.Mapping.Vector3 center)
		{
			smethod_3(BaseUI.Get().GetBnetCamera(), center, bool_0: true);
		}

		public static void MouseOver(int cx, int cy)
		{
			smethod_0(cx, cy, bool_0: false);
		}

		public static void MouseOver(Triton.Game.Mapping.Vector3 center)
		{
			smethod_2(Camera.Main, center, bool_0: false);
		}

		public static void MouseOverDialog(Triton.Game.Mapping.Vector3 center)
		{
			smethod_2(BaseUI.Get().GetBnetCamera(), center, bool_0: false);
		}

		public static Triton.Game.Mapping.Vector3 CardInteractPoint(Card card)
		{
			Actor actor = card.GetActor();
			if (actor == null)
			{
				return new Triton.Game.Mapping.Vector3(0f, 0f, 0f);
			}
			return actor.m_costTextMesh?.m_TextMeshGameObject.Renderer.Bounds.m_Center ?? actor.Transform.Position;
		}

		public static async Task MoveCursorHumanLike(Triton.Game.Mapping.Vector3 center)
		{
			int height = Screen.Height;
			Triton.Game.Mapping.Vector3 vector = Camera.Main.WorldToScreenPoint(center);
			vector.Y = height - (int)vector.Y;
			ProcessHookManager.ReadScreenToClient(out var x, out var y, out var result);
			if (x < 0 || y < 0 || x >= Screen.Width || y >= height)
			{
				ProcessHookManager.Reset();
				ProcessHookManager.ReadScreenToClient(out x, out y, out result);
			}
			Vector2i start = new Vector2i(x, y);
			Vector2i end = new Vector2i((int)vector.X, (int)vector.Y);
			List<Vector2i> pointsOnSegment = Misc.GetPointsOnSegment(start, end);
			int num = pointsOnSegment.Count / 4;
			if (num < 1)
			{
				num = 1;
			}
			for (int i = 0; i < pointsOnSegment.Count; i += num)
			{
				Vector2i vector2i = pointsOnSegment[i];
				PegUI.Get()?.OnAppFocusChanged(focus: true, intptr_0);
				Input.SetMousePos(vector2i.X, vector2i.Y);
				await Coroutine.Sleep(1);
			}
		}
	}
}
